package easier.sectionheader.decoration;

import android.content.Context;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import easier.sectionheader.decoration.AbsSlideIndexer.Builder;
import easier.sectionheader.decoration.AbsSlideIndexer.onScrollListener;

public class IndexerFactory {

    public static < T > void quickAttach(
        RecyclerView recyclerView,
        SectionDataIndexer< T > section,
        AbsSlideIndexer slideIndexer,
        @ColorInt int colorHeaderBackgroud,
        @ColorInt int colorHeaderText,
        float headerTextXOffset
    ) {

        StickyHeaderDecoration stickyHeaderDecoration = new StickyHeaderDecoration( recyclerView.getContext(), section ) {

            @Override
            protected void initHeaderDecoration( Context context ) {

                super.initHeaderDecoration( context );
                mHeaderTextPaint.setColor( colorHeaderText );
                mHeaderBgPaint.setColor( colorHeaderBackgroud );

                mTextXOffset = headerTextXOffset;
            }
        };

        recyclerView.addItemDecoration( stickyHeaderDecoration );

        slideIndexer.attachToRecyclerView( recyclerView, new onScrollListener() {
            @Override
            public void onScrolled( RecyclerView rv, int sectionIndex ) {

                int positionForSection = section.getPositionForSection( sectionIndex );
                if ( positionForSection == -1 ) {
                    return;
                }
                RecyclerView.LayoutManager layoutManager = rv.getLayoutManager();
                if ( layoutManager instanceof LinearLayoutManager ) {
                    LinearLayoutManager linearLayoutManager = (LinearLayoutManager) layoutManager;
                    linearLayoutManager.scrollToPositionWithOffset( positionForSection, 0 );
                }
            }

            @Override
            public void onRecyclerScrolled( @NonNull RecyclerView recyclerView, int dx, int dy ) {

                String showed = stickyHeaderDecoration.getFirstIndexShowed();
                slideIndexer.showSectionAtTouchScroll( showed );
            }
        } );
    }

    public static < T > void quickAttachBallIndexer(
        RecyclerView recyclerView,
        SectionDataIndexer< T > section,
        @ColorInt int colorHeaderBackgroud,
        @ColorInt int colorHeaderText,
        float headerTextXOffset ) {

        StringBuilder sb = new StringBuilder();
        String[] sections = section.getSections();
        for ( String s : sections ) {
            sb.append( s );
        }

        BalloonIndexer balloonIndexer = new BalloonIndexer( new Builder( recyclerView.getContext(), sb.toString() ) );

        quickAttach( recyclerView, section, balloonIndexer, colorHeaderBackgroud, colorHeaderText, headerTextXOffset );
    }

    public static < T > void quickAttachBallIndexer( RecyclerView recyclerView,
        SectionDataIndexer< T > section, float headerTextXOffset ) {

        quickAttachBallIndexer( recyclerView, section, 0xFFEAEAEA, 0xFF333333, headerTextXOffset );
    }

    public static < T > void quickAttachCircleIndexer(
        RecyclerView recyclerView,
        SectionDataIndexer< T > section,
        @ColorInt int colorHeaderBackgroud,
        @ColorInt int colorHeaderText,
        float headerTextXOffset,
        @ColorInt int colorSlideBackgroud,
        @ColorInt int colorSlideText ) {

        StringBuilder sb = new StringBuilder();
        String[] sections = section.getSections();
        for ( String s : sections ) {
            sb.append( s );
        }

        CircleIndexer balloonIndexer = new CircleIndexer(
            new Builder( recyclerView.getContext(), sb.toString() ),
            colorSlideBackgroud,
            colorSlideText
        );

        quickAttach( recyclerView, section, balloonIndexer, colorHeaderBackgroud, colorHeaderText, headerTextXOffset );
    }

    public static < T > void quickAttachCircleIndexer(
        RecyclerView recyclerView,
        SectionDataIndexer< T > section,
        float headerTextXOffset,
        @ColorInt int colorSlideBackgroud,
        @ColorInt int colorSlideText ) {

        quickAttachCircleIndexer(
            recyclerView,
            section,
            0xFFEAEAEA,
            0xFF333333,
            headerTextXOffset,
            colorSlideBackgroud,
            colorSlideText
        );
    }

    public static < T > void quickAttachCircleIndexer( RecyclerView recyclerView,
        SectionDataIndexer< T > section, float headerTextXOffset ) {

        quickAttachCircleIndexer(
            recyclerView,
            section,
            0xFFEAEAEA,
            0xFF333333,
            headerTextXOffset,
            0xFFEAEAEA,
            0xFF333333
        );
    }

    public static < T > void quickAttachSquareIndexer(
        RecyclerView recyclerView,
        SectionDataIndexer< T > section,
        @ColorInt int colorHeaderBackgroud,
        @ColorInt int colorHeaderText,
        float headerTextXOffset ) {

        StringBuilder sb = new StringBuilder();
        String[] sections = section.getSections();
        for ( String s : sections ) {
            sb.append( s );
        }

        SquareIndexer balloonIndexer = new SquareIndexer(
            new Builder( recyclerView.getContext(), sb.toString() )
        );

        quickAttach( recyclerView, section, balloonIndexer, colorHeaderBackgroud, colorHeaderText, headerTextXOffset );
    }

    public static < T > void quickAttachSquareIndexer(
        RecyclerView recyclerView,
        SectionDataIndexer< T > section,
        float headerTextXOffset ) {

        quickAttachSquareIndexer( recyclerView, section, 0xFFEAEAEA, 0xFF333333, headerTextXOffset );
    }
}
